package com.xjrsoft.module.language.controller;


import com.alibaba.fastjson.JSON;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.module.language.dto.LgMapDto;
import com.xjrsoft.module.language.dto.LgMapListDto;
import com.xjrsoft.module.language.entity.XjrLgMark;
import com.xjrsoft.module.language.service.IXjrLgMapService;
import com.xjrsoft.module.language.service.IXjrLgMarkService;
import com.xjrsoft.module.language.vo.LgMapNameVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 语言映照表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-11-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/lp-map")
@Api(value = "/lg-map", tags = "翻译模块")
public class XjrLgMapController {

    private final IXjrLgMarkService lgMarkService;

    private IXjrLgMapService lgMapService;

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/12
     * @Param:[id]
     * @return:com.xjrsoft.common.result.Response
     * @Description:获取详情
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取详情")
    public Response getLgMapById(@PathVariable String id) {
        return Response.ok(lgMapService.getById(id));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/12
     * @Param:[id]
     * @return:com.xjrsoft.common.result.Response
     * @Description:修改
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "修改")
    public Response update(@PathVariable String id, @RequestBody Map<String, String> mapDto) {
        return Response.status(lgMapService.updateLgMap(id, mapDto));
    }

    /**
     * @Author:湘北智造-框架开发组
     * @Date:2020/11/12
     * @Param:[objectId, keyword]
     * @return:com.xjrsoft.common.result.Response
     * @Description:新增翻译标记和翻译
     */
    @PostMapping
    @ApiOperation(value = "根据翻译标记新增翻译")
    public Response addLgMap(@RequestBody LgMapDto lgMapDto) {
        // 唯一校验
        String enCode = lgMapDto.getEnCode();
        if (!lgMarkService.isUnique(null, enCode)) {
            return Response.notOk("翻译标记编码(" + enCode + ")已存在！");
        }
        XjrLgMark lgMark = BeanUtil.copy(lgMapDto, XjrLgMark.class);
        Map<String, String> lgMapList = lgMapDto.getLgMapList();
        return Response.status(lgMapService.saveLgMapWithMark(lgMark, lgMapList));
    }

    @GetMapping
    @ApiOperation(value = "获取列表数据，分页")
    public Response<PageOutput<Map<String, Object>>> getPageList(LgMapListDto lgMapListDto) {
        return Response.ok(lgMapService.getPageList(lgMapListDto));
    }

    @GetMapping("/{typeCode}/name")
    @ApiOperation(value = "根据类型查询翻译名")
    public Response<String> getMapNameTreeByCode(@PathVariable String typeCode) {
        Map<String, Object> resultMap = new HashMap<>();
        List<LgMapNameVo> mapNameVoList = lgMapService.selectNamesTreeByCode(typeCode);
        if (CollectionUtil.isNotEmpty(mapNameVoList)) {
            for (LgMapNameVo mapNameTreeVo : mapNameVoList) {
                String mkEnCode = mapNameTreeVo.getMkEnCode();
                String mapName = mapNameTreeVo.getMapName();
                resultMap.put(mkEnCode, mapName);
            }
        }

        return Response.ok(JSON.toJSONString(resultMap));
    }
}
